package com.itheima.easy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.easy.constant.SuperConstant;
import com.itheima.easy.mapper.TradingMapper;
import com.itheima.easy.pojo.Trading;
import com.itheima.easy.service.ITradingService;
import com.itheima.easy.utils.EmptyUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description：交易订单表 服务实现类
 */
@Service
public class TradingServiceImpl extends ServiceImpl<TradingMapper, Trading> implements ITradingService {

    @Override
    public Trading findTradByTradingOrderNo(Long tradingOrderNo) {
        QueryWrapper<Trading> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Trading::getTradingOrderNo,tradingOrderNo);
        return getOne(queryWrapper);
    }

    @Override
    public Trading findTradByProductOrderNo(Long productOrderNo) {
        LambdaQueryWrapper<Trading> queryWrapper = Wrappers.<Trading>lambdaQuery();
        queryWrapper.eq(Trading::getProductOrderNo,productOrderNo).orderByDesc(Trading::getCreatedTime);
        List<Trading> list = list(queryWrapper);
        if (!EmptyUtil.isNullOrEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<Trading> findTradingByTradingState(String tradingState) {
        QueryWrapper<Trading> queryWrapper = new QueryWrapper<>();
        LocalDateTime localDateTime =  LocalDateTime.now().minusSeconds(600);
        queryWrapper.lambda().eq(Trading::getTradingState,tradingState)
            .gt(Trading::getCreatedTime, localDateTime)
            .eq(Trading::getEnableFlag, SuperConstant.YES);
        return list(queryWrapper);
    }
}
